<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>WebSocket测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .status {
            padding: 10px;
            margin: 10px 0;
            border-radius: 5px;
        }
        .connected {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .disconnected {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .message {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
            padding: 10px;
            margin: 5px 0;
            border-radius: 5px;
        }
        button {
            padding: 10px 20px;
            margin: 5px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        .btn-primary {
            background-color: #007bff;
            color: white;
        }
        .btn-success {
            background-color: #28a745;
            color: white;
        }
        .btn-warning {
            background-color: #ffc107;
            color: #212529;
        }
        #messageLog {
            height: 300px;
            overflow-y: auto;
            border: 1px solid #ccc;
            padding: 10px;
            background-color: #f8f9fa;
        }
    </style>
</head>
<body>
    <h1>WebSocket连接测试</h1>
    
    <div id="status" class="status disconnected">
        状态: 未连接
    </div>
    
    <div>
        <button class="btn-primary" onclick="connect()">连接WebSocket</button>
        <button class="btn-warning" onclick="disconnect()">断开连接</button>
        <button class="btn-success" onclick="sendAuth()">发送认证</button>
        <button class="btn-success" onclick="testNotification()">测试通知</button>
    </div>
    
    <h3>消息日志:</h3>
    <div id="messageLog"></div>
    
    <script>
        let ws = null;
        let isConnected = false;
        
        function updateStatus(connected, message) {
            const statusDiv = document.getElementById('status');
            isConnected = connected;
            
            if (connected) {
                statusDiv.className = 'status connected';
                statusDiv.textContent = '状态: 已连接 - ' + message;
            } else {
                statusDiv.className = 'status disconnected';
                statusDiv.textContent = '状态: 未连接 - ' + message;
            }
        }
        
        function addMessage(message, type = 'info') {
            const logDiv = document.getElementById('messageLog');
            const time = new Date().toLocaleTimeString();
            const messageDiv = document.createElement('div');
            messageDiv.className = 'message';
            messageDiv.innerHTML = `<strong>[${time}]</strong> ${message}`;
            logDiv.appendChild(messageDiv);
            logDiv.scrollTop = logDiv.scrollHeight;
        }
        
        function connect() {
            try {
                const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
                const host = window.location.host;
                const wsUrl = `${protocol}//${host}/ws`;
                
                addMessage(`正在连接到: ${wsUrl}`);
                
                ws = new WebSocket(wsUrl);
                
                ws.onopen = function(event) {
                    updateStatus(true, '连接成功');
                    addMessage('WebSocket连接已建立');
                };
                
                ws.onmessage = function(event) {
                    addMessage(`收到消息: ${event.data}`);
                    try {
                        const data = JSON.parse(event.data);
                        addMessage(`解析后的消息: ${JSON.stringify(data, null, 2)}`);
                    } catch (error) {
                        addMessage(`消息解析失败: ${error.message}`);
                    }
                };
                
                ws.onclose = function(event) {
                    updateStatus(false, '连接已关闭');
                    addMessage(`WebSocket连接已关闭，代码: ${event.code}, 原因: ${event.reason}`);
                };
                
                ws.onerror = function(error) {
                    updateStatus(false, '连接错误');
                    addMessage(`WebSocket连接错误: ${error}`);
                };
                
            } catch (error) {
                addMessage(`连接失败: ${error.message}`);
                updateStatus(false, '连接失败');
            }
        }
        
        function disconnect() {
            if (ws) {
                ws.close();
                ws = null;
                updateStatus(false, '手动断开');
                addMessage('手动断开WebSocket连接');
            }
        }
        
        function sendAuth() {
            if (!ws || !isConnected) {
                addMessage('错误: WebSocket未连接');
                return;
            }
            
            const authMessage = {
                userId: 'test_user_' + Date.now(),
                messageType: 'Student_Auth',
                title: '',
                content: '',
                param: ''
            };
            
            const messageStr = JSON.stringify(authMessage);
            ws.send(messageStr);
            addMessage(`发送认证消息: ${messageStr}`);
        }
        
        function testNotification() {
            if (!ws || !isConnected) {
                addMessage('错误: WebSocket未连接');
                return;
            }
            
            fetch('/api/test/websocket/notify-students', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    title: '测试通知',
                    content: '这是一条来自测试页面的通知'
                })
            })
            .then(response => response.json())
            .then(data => {
                addMessage(`测试通知API响应: ${JSON.stringify(data)}`);
            })
            .catch(error => {
                addMessage(`测试通知API错误: ${error.message}`);
            });
        }
        
        // 页面加载时自动连接
        window.onload = function() {
            addMessage('页面加载完成，准备连接WebSocket...');
            setTimeout(connect, 1000);
        };
    </script>
</body>
</html> 